package com.eminem.dao;

import com.eminem.common.StrMath;
import com.eminem.dao.base.BaseDao;
import com.eminem.model.Article;
import com.eminem.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArticleDao extends BaseDao<Article> {
    public ArticleDao() {
        super(Article.class);
    }


    public List<Map<String, Object>> getArticleAll(String userId,Integer currentPage,String name) {
        List<Map<String, Object>> map = new ArrayList<>();
        String sql = "SELECT a.*,f.id as fid,(select count(*) from favorite f where f.a_id = a.id) as ArticleCount\n" +
                "FROM article a\n" +
                "left join favorite f on a.id = f.id\n" +
                "limit ?,?";
        if (null != name){
            sql = "SELECT a.*,f.id as fid,(select count(*) from favorite f where f.a_id = a.id) as ArticleCount\n" +
                    "FROM article a\n" +
                    "left join favorite f on a.id = f.id\n" +
                    "where a.title like ?\n" +
                    "limit ?,?";
        }

        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null != name){
                map = qr.query(sql,new MapListHandler(),"%"+name+"%",(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
            }else {
                map = qr.query(sql,new MapListHandler(),(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;

    }

    public long getArticleCount(String name) {
        long count = 0L;
        String sql = "select count(*) as ArticleCount from article";
        if (null != name){
            sql = "select count(*) as ArticleCount from article where title like ?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = null;
        try {
            if (null != name){
                map = qr.query(sql,new MapHandler(),"%"+name+"%");
            }else {
                map = qr.query(sql,new MapHandler());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null != map){
            count = Long.parseLong(map.get("ArticleCount").toString());
        }
        return count;

    }

    public Map<String, Object> getArticleList(String id) {
        Map<String, Object> map = new HashMap<>();
        String sql = "SELECT a.*,u.id as uId,f.u_id,f.id as fid,(select count(*) from favorite f where f.a_id = a.id) as ArticleCount\n" +
                "FROM article a\n" +
                "left join favorite f on a.id = f.a_id\n" +
                "left join user u on u.id = f.u_id\n" +
                "where a.id = ?";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map =  qr.query(sql,new MapHandler(),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }

    public List<Map<String, Object>> findArticle(Integer currentPage,String page,String name) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        String sql = "select u.id,f.u_id,f.a_id,a.title,a.content,a.browse_count,a.publish_date,a.publish_username,a.user_id,(select count(*) from favorite f where f.a_id = a.id) as ArticleCount\n" +
                "from user u\n" +
                "left join favorite f on f.u_id = u.id\n" +
                "left join article a on a.id = f.a_id\n" +
                "where u.id = ?\n" +
                "limit ?,?";
        if (null != name) {
            sql = "select u.id,f.u_id,f.a_id,a.title,a.content,a.browse_count,a.publish_date,a.publish_username,a.user_id,(select count(*) from favorite f where f.a_id = a.id) as ArticleCount\n" +
                    "from user u\n" +
                    "left join favorite f on f.u_id = u.id\n" +
                    "left join article a on a.id = f.a_id\n" +
                    "where u.id = ? and a.title like ?\n" +
                    "limit ?,?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null != name){
                mapList = qr.query(sql,new MapListHandler(),page,"%"+name+"%",(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
            }else {
                mapList = qr.query(sql,new MapListHandler(),page,(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return mapList;
    }

    public long getCount(String Id,String name) {
        long count = 0L;
        String sql = "select count(*) as CollectionCount from favorite where u_id = ?";
        if (null != name){
            sql = "select count(*) as CollectionCount \n" +
                    "from favorite f\n" +
                    "left join article a on a.id = f.a_id\n" +
                    "where u_id = ? and title like ?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = null;
        try {
            if (null != name){
                map = qr.query(sql,new MapHandler(),Id,"%"+name+"%");
            }else {
                map = qr.query(sql,new MapHandler(),Id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null != map){
            count = Long.parseLong(map.get("CollectionCount").toString());
        }
        return count;

    }

//    public int AddArticle(Article article) {
//        int row = 0;
//
//        return row;
//    }

}
